﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace HobSoft.SoccerBet2014.Wcf
{
    using Domain.Core;
    using Domain.IRepositories;
    using Domain.Models;
    using Domain.Models.KeyHelper;
    using Domain.ClientService;

    public class BetCustomDataService : IBetCustomDataService
    {
        private readonly IGameRepository gameRepository;
        private readonly IOddsRepository oddsRepository;
        private readonly IGameMappingRepository gameMappingRepository;

        public BetCustomDataService(IGameRepository _gameRepository
                                            , IOddsRepository _oddsRepository
                                            , IGameMappingRepository _gameMappingRepository)
        {
            gameRepository = _gameRepository;
            oddsRepository = _oddsRepository;
            gameMappingRepository = _gameMappingRepository;
        }

        public IList<Game> GetLiveGames(DateTime? lastUpdateTime)
        {
            var startTime = DateTime.Now.AddHours(-3);
            var beforeTime = DateTime.Now.AddMinutes(5);
            var mapping = gameMappingRepository.GetMany(startTime, beforeTime);
            var keys = mapping.Select(m => KeyGenerateHelper.Create<Game>(m.BmID, m.GameID));
            var uniKeyRepository = gameMappingRepository as IUniKeyReadRepository<Game>;
            IList<Game> mappingGames = new List<Game>();
            if (uniKeyRepository != null)
                mappingGames = uniKeyRepository.GetMany(keys);
            else
                mappingGames = gameRepository.GetMany(g => keys.Contains(g.Key));

            if (!lastUpdateTime.HasValue)
                return mappingGames.Where(g => g.State != Game.GameState.Finish && g.StartTime.HasValue && g.StartTime < DateTime.Now.AddHours(-3)).ToList();

            return mappingGames.Where(g => g.LastUpdateTime > lastUpdateTime.Value && (g.State == Game.GameState.InProgress || (g.StartTime.HasValue && g.StartTime < DateTime.Now))).ToList();
        }

        public IList<Odds> GetNewOdds(params string[] marketKeys)
        {
            return oddsRepository.GetNewByMarket(marketKeys);
        }

        public IList<Odds> GetTypeNewOdds(Odds.MarketType type, params string[] marketKeys)
        {
            return oddsRepository.GetNewByMarket(type, marketKeys);
        }

        public IList<Game> GetEarlyGames()
        {
            var startTime = DateTime.Now.AddHours(-3);
            var beforeTime = DateTime.Now.AddHours(24);
            var lastUpdate=DateTime.Now.AddMinutes(-5);
            var mapping = gameMappingRepository.GetMany(startTime, beforeTime);
            var keys = mapping.Select(m => KeyGenerateHelper.Create<Game>(m.BmID, m.GameID)).ToArray();
            var uniKeyRepository = gameRepository as IUniKeyReadRepository<Game>;
            IList<Game> result = null;
            if (uniKeyRepository != null)
                result = uniKeyRepository.GetMany(keys);
            else
                result = gameRepository.GetMany(g => keys.Contains(g.Key));
            return result.Where(g => g.LastUpdateTime > lastUpdate || g.State == Game.GameState.PreGame).ToList();
        }

    }
}
